#!/bin/sh

# Various commands to help with AxTk documentation and distribution.

# If your zip accepts Cygwin-style paths, then
# use cygpath, else substitute echo

PROGNAME=$0
PLATFORM=

if [ "$OS" = "Windows_NT" ]; then
    PLATFORM=Windows
    AXTKDIR=c:/projects/axtk
else
    if [ "$OSTYPE" = "darwin" ] || [ "$OSTYPE" = "darwin8.0" ] || [ "$OSTYPE" = "darwin9.0" ]; then
        PLATFORM=Mac
        AXTKDIR=/Users/juliansm/axtk
    else
        PLATFORM=Linux
        AXTKDIR=/home/julians/axtk
    fi
fi

SCRIPTDIR=$AXTKDIR/scripts
HTMLFTPSITE=ftp://www.anthemion.co.uk/public_html

VERSION=
FTPUSER=
FTPPWD=

NEED_LOGIN=0
COPYHTMLTOFTP=0
RUN_DOXYGEN_HTML=0
UPLOAD_ZIP=0
UPLOAD_ZIP_SAMPLES=0
UPLOAD_LINUX_SAMPLES=0
UPLOAD_MAC_SAMPLES=0
MAKE_ZIP=0
MAKE_TARBALL=0
MAKE_ZIP_SAMPLES=0
MAKE_LINUX_SAMPLES=0
MAKE_MAC_SAMPLES=0
DATESTAMP=`date +%Y-%m-%d` 

doreplace()
{
    thefile=$1
    theexpr=$2

    if [ -f $thefile ]; then
      sed -e "$theexpr" < $thefile > $thefile.tmp
      mv $thefile.tmp $thefile
    else
      echo "*** $thefile not found."
    fi
}

uploadfile()
{
    thesourcefile=$1
    thedestfile=$2

	curl $HTMLFTPSITE/$thedestfile --user "$FTPUSER:$FTPPWD" --upload-file $thesourcefile
}

unix2dosname()
{
    echo $1 | sed -e "s/\//\\\\\\\/g" > /tmp/filename.tmp
    RETVALUE=`cat /tmp/filename.tmp`
    rm -f /tmp/filename.tmp
}

unix2dosname2()
{
    echo $1 | sed -e "s/\//\\\\/g" > /tmp/filename.tmp
    RETVALUE=`cat /tmp/filename.tmp`
    rm -f /tmp/filename.tmp
}

# Copy HTML to ftp site
copy_html_to_ftp_site()
{
    echo Creating tarball...
    cd $AXTKDIR/docs/manual/html
    rm -f axtk_html.tar.gz
    tar cf axtk_html.tar.gz *.html *.png *.gif
    
    echo Copying axtk_html.tar.gz to ftp site...
    uploadfile axtk_html.tar.gz axtk/html/axtk_html.tar.gz
    
    echo Untarring remotely...
    PATH=/c/apps/putty:$PATH
	plink "Anthemion Dolphin" -l $FTPUSER -pw $FTPPWD "cd /home/anthemion/public_html/axtk/html ; tar xf axtk_html.tar.gz"

    echo Done.    
}

make_zip()
{
    DESTDIR=$AXTKDIR/deliver
    
    mkdir -p $DESTDIR
    rm -f $DESTDIR/axtk-$DATESTAMP.zip
    cd $AXTKDIR/..
    zip $DESTDIR/axtk-$DATESTAMP.zip -@ < $AXTKDIR/scripts/files.txt
}

make_tarball()
{
    DESTDIR=$AXTKDIR/deliver
    
    mkdir -p $DESTDIR
    rm -f $DESTDIR/axtk*.tar.gz
    cd $AXTKDIR/..
    expandlines $AXTKDIR/scripts/files.txt /tmp/files.out

    tar cfz $DESTDIR/axtk-$DATESTAMP.tar.gz -T /tmp/files.out
    
    rm -f /tmp/files.out
}

copy_sample_common_files()
{
    DESTDIR=$AXTKDIR/deliver

    SAMPLEDIR=$DESTDIR/axtk-basic-sample
    cp $AXTKDIR/samples/ax/sample/*.dsp $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/*.dsw $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/Makefile $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/makefile.gcc $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/*.rc $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/axsample.pjd $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/*.xpm $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/*.png $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/readme.txt $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/*.cpp $SAMPLEDIR
    cp $AXTKDIR/samples/ax/sample/*.h $SAMPLEDIR

    cp $AXTKDIR/docs/readme.txt $SAMPLEDIR/axtk-readme.txt
    cp $AXTKDIR/docs/todo.txt $SAMPLEDIR
    cp $AXTKDIR/docs/license.txt $SAMPLEDIR
    
    SAMPLEDIR=$DESTDIR/axtk-resource-sample

    cp $AXTKDIR/samples/ax_resource/resource_sample/*.dsp $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/*.dsw $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/Makefile $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/makefile.gcc $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/*.rc $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/axresourcesample.pjd $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/*.xpm $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/*.png $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/readme.txt $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/*.cpp $SAMPLEDIR
    cp $AXTKDIR/samples/ax_resource/resource_sample/*.h $SAMPLEDIR

    cp $AXTKDIR/docs/readme.txt $SAMPLEDIR/axtk-readme.txt
    cp $AXTKDIR/docs/todo.txt $SAMPLEDIR
    cp $AXTKDIR/docs/license.txt $SAMPLEDIR    
}

make_zip_samples()
{
    DESTDIR=$AXTKDIR/deliver
    
    rm -f $DESTDIR/axtk-basic-sample-win32.zip
    rm -f $DESTDIR/axtk-resource-sample-win32.zip

    rm -f -r $DESTDIR/axtk-basic-sample
    mkdir -p $DESTDIR/axtk-basic-sample
    
    rm -f -r $DESTDIR/axtk-resource-sample
    mkdir -p $DESTDIR/axtk-resource-sample

    copy_sample_common_files
    
    SAMPLEDIR=$DESTDIR/axtk-basic-sample
    cp $AXTKDIR/samples/ax/sample/VCProjectUnicodeRelease/axsample.exe $SAMPLEDIR
    
    cd $SAMPLEDIR/..
    zip $DESTDIR/axtk-basic-sample-win32.zip axtk-basic-sample/*

    rm -f -r $SAMPLEDIR

    SAMPLEDIR=$DESTDIR/axtk-resource-sample

    cp $AXTKDIR/samples/ax_resource/resource_sample/VCProjectUnicodeRelease/axresourcesample.exe $SAMPLEDIR
    
    cd $SAMPLEDIR/..
    zip $DESTDIR/axtk-resource-sample-win32.zip axtk-resource-sample/*
    rm -f -r $SAMPLEDIR
}

make_linux_samples()
{
    DESTDIR=$AXTKDIR/deliver
    
    rm -f $DESTDIR/axtk-basic-sample-linux.tar.gz
    rm -f $DESTDIR/axtk-resource-sample-linux.tar.gz

    rm -f -r $DESTDIR/axtk-basic-sample
    mkdir -p $DESTDIR/axtk-basic-sample
    
    rm -f -r $DESTDIR/axtk-resource-sample
    mkdir -p $DESTDIR/axtk-resource-sample

    copy_sample_common_files
    
    SAMPLEDIR=$DESTDIR/axtk-basic-sample
    cp $AXTKDIR/samples/ax/sample/GCCUnicodeReleaseGTK/axsample $SAMPLEDIR
    strip $SAMPLEDIR/axsample
    
    cd $SAMPLEDIR/..
    tar cfzv $DESTDIR/axtk-basic-sample-linux.tar.gz axtk-basic-sample/*

    rm -f -r $SAMPLEDIR

    SAMPLEDIR=$DESTDIR/axtk-resource-sample

    cp $AXTKDIR/samples/ax_resource/resource_sample/GCCUnicodeReleaseGTK/axresourcesample $SAMPLEDIR
    strip $SAMPLEDIR/axresourcesample
    
    cd $SAMPLEDIR/..
    tar cfzv $DESTDIR/axtk-resource-sample-linux.tar.gz axtk-resource-sample/*
    rm -f -r $SAMPLEDIR
}

make_mac_samples()
{
    DESTDIR=$AXTKDIR/deliver
    
    rm -f $DESTDIR/axtk-basic-sample-mac.tar.gz
    rm -f $DESTDIR/axtk-resource-sample-mac.tar.gz

    rm -f -r $DESTDIR/axtk-basic-sample
    mkdir -p $DESTDIR/axtk-basic-sample
    
    rm -f -r $DESTDIR/axtk-resource-sample
    mkdir -p $DESTDIR/axtk-resource-sample

    copy_sample_common_files
    
    SAMPLEDIR=$DESTDIR/axtk-basic-sample
    cp -r $AXTKDIR/samples/ax/sample/GCCUnicodeReleaseMac/axsample.app $SAMPLEDIR
    #strip $SAMPLEDIR/axsample
    
    cd $SAMPLEDIR/..
    tar cfzv $DESTDIR/axtk-basic-sample-mac.tar.gz axtk-basic-sample/*

    rm -f -r $SAMPLEDIR

    SAMPLEDIR=$DESTDIR/axtk-resource-sample

    cp -r $AXTKDIR/samples/ax_resource/resource_sample/GCCUnicodeReleaseGTK/axresourcesample.app $SAMPLEDIR
    # strip $SAMPLEDIR/axresourcesample
    
    cd $SAMPLEDIR/..
    tar cfzv $DESTDIR/axtk-resource-sample-mac.tar.gz axtk-resource-sample/*
    rm -f -r $SAMPLEDIR
}

upload_zip_to_ftp_site()
{
    DESTDIR=$AXTKDIR/deliver
    
    uploadfile $DESTDIR/axtk-$DATESTAMP.zip axtk/axtk-$DATESTAMP.zip
}

upload_tarball_to_ftp_site()
{
    DESTDIR=$AXTKDIR/deliver
    
    uploadfile $DESTDIR/axtk-$DATESTAMP.tar.gz axtk/axtk-$DATESTAMP.tar.gz
}

upload_zip_samples_to_ftp_site()
{
    DESTDIR=$AXTKDIR/deliver
    
    uploadfile $DESTDIR/axtk-basic-sample-win32.zip axtk/axtk-basic-sample-win32.zip
    uploadfile $DESTDIR/axtk-resource-sample-win32.zip axtk/axtk-resource-sample-win32.zip
}

upload_linux_samples_to_ftp_site()
{
    DESTDIR=$AXTKDIR/deliver
    
    uploadfile $DESTDIR/axtk-basic-sample-linux.tar.gz axtk/axtk-basic-sample-linux.tar.gz
    uploadfile $DESTDIR/axtk-resource-sample-linux.tar.gz axtk/axtk-resource-sample-linux.tar.gz
}

upload_mac_samples_to_ftp_site()
{
    DESTDIR=$AXTKDIR/deliver
    
    uploadfile $DESTDIR/axtk-basic-sample-mac.tar.gz axtk/axtk-basic-sample-mac.tar.gz
    uploadfile $DESTDIR/axtk-resource-sample-mac.tar.gz axtk/axtk-resource-sample-mac.tar.gz
}

get_ftp_login()
{
    echo -n "FTP login: "
    read -e FTPUSER
    echo -n "FTP Password: "
    read -e FTPPWD
}

# Create HTML manual using Doxygen
run_doxygen_html()
{
    cd $AXTKDIR/docs/manual
    doxygen Doxyfile
}

# We can't use e.g. this:
# ls `cat $SRC/scripts/msw/makefile.rsp` zip -@ -u $DEST/wxWindows-$VERSION-gen.zip
# because there's not enough space on the command line, plus we need to ignore the
# blank lines.
# So if we need to (not in this script so far) we do something like this instead:
# expandlines $SRC/setup/files.rsp temp.txt
# zip -@ `$CYGPATHPROG -w $DEST/archive.zip` < temp.txt

expandlines()
{
    toexpand=$1
    outputfile=$2

    rm -f $outputfile
    touch $outputfile
    for line in `cat $toexpand` ; do
      if [ "$line" != "" ]; then
        ls $line >> $outputfile
      fi
    done
}

usage()
{
    echo "Usage: $PROGNAME [ options ]" 1>&2
    echo Options:
    echo "    --help                Display this help message"
    echo "    --make-html           Create the HTML manual using Doxygen"
    echo "    --make-zip            Create a zip source archive"
    echo "    --make-tarball        Create a tarball source archive"
    echo "    --make-zip-samples    Create a zip source archive of the samples"
    echo "    --make-linux-samples  Create a Linux tarball of the samples"
    echo "    --make-mac-samples    Create a Mac tarball of the samples"
    echo "    --update-docs         Create and upload the HTML manual"
    echo "    --ftp-html            Copy the HTML manual to the web site"
    echo "    --ftp-zip             Upload a zip snapshot"
    echo "    --ftp-zip-samples     Upload zipped samples"
    echo "    --ftp-linux-samples   Upload Linux samples"
    echo "    --ftp-mac-samples     Upload Mac samples"
    echo "    --ftp-tarball         Upload the source tarball"
    echo ""
    exit 1
}

# Process command line options.

for i in "$@"; do
    case "$i" in
	--ftp-html) COPYHTMLTOFTP=1; NEED_LOGIN=1 ;;
	--ftp-zip) UPLOAD_ZIP=1; NEED_LOGIN=1 ;;
	--ftp-tarball) UPLOAD_TARBALL=1; NEED_LOGIN=1 ;;
	--ftp-zip-samples) UPLOAD_ZIP_SAMPLES=1; NEED_LOGIN=1 ;;
	--ftp-linux-samples) UPLOAD_LINUX_SAMPLES=1; NEED_LOGIN=1 ;;
	--ftp-mac-samples) UPLOAD_MAC_SAMPLES=1; NEED_LOGIN=1 ;;
	--make-html) RUN_DOXYGEN_HTML=1 ;;
	--make-zip) MAKE_ZIP=1 ;;
	--make-tarball) MAKE_TARBALL=1 ;;
	--make-zip-samples) MAKE_ZIP_SAMPLES=1 ;;
	--make-linux-samples) MAKE_LINUX_SAMPLES=1 ;;
	--make-mac-samples) MAKE_MAC_SAMPLES=1 ;;
	--update-docs) RUN_DOXYGEN_HTML=1; COPYHTMLTOFTP=1 NEED_LOGIN=1 ;;
	*)
	    usage
	    exit
	    ;;
    esac
done

if [ "$NEED_LOGIN" = "1" ]; then
    get_ftp_login
fi

if [ "$RUN_DOXYGEN_HTML" = "1" ]; then
    run_doxygen_html
fi

if [ "$MAKE_ZIP" = "1" ] || [ "$UPLOAD_ZIP" = "1" ] ; then
    make_zip
fi

if [ "$MAKE_TARBALL" = "1" ] || [ "$UPLOAD_TARBALL" = "1" ] ; then
    make_tarball
fi

if [ "$MAKE_ZIP_SAMPLES" = "1" ] ; then
    make_zip_samples
fi

if [ "$MAKE_LINUX_SAMPLES" = "1" ] ; then
    make_linux_samples
fi

if [ "$MAKE_MAC_SAMPLES" = "1" ] ; then
    make_mac_samples
fi

if [ "$COPYHTMLTOFTP" = "1" ]; then
    copy_html_to_ftp_site
fi

if [ "$UPLOAD_ZIP" = "1" ]; then
    upload_zip_to_ftp_site
fi

if [ "$UPLOAD_TARBALL" = "1" ]; then
    upload_tarball_to_ftp_site
fi

if [ "$UPLOAD_ZIP_SAMPLES" = "1" ]; then
    upload_zip_samples_to_ftp_site
fi

if [ "$UPLOAD_LINUX_SAMPLES" = "1" ]; then
    upload_linux_samples_to_ftp_site
fi

if [ "$UPLOAD_MAC_SAMPLES" = "1" ]; then
    upload_mac_samples_to_ftp_site
fi
